<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class penduduk extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->load->library("pagination");
        $this->load->model('penduduk_model');
    }

    public function index() {
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar');
        $list_kecamatan = $this->penduduk_model->get_list_kecamatan();
        $this->load->view('page/v_jenis_kelamin', array('list_kecamatan' => $list_kecamatan));
        $this->load->view('theme/footer');
        $this->load->view('js/js_pekerjaan', array('chart' => 'penduduk/chart'));
    }

    function persen_daerah() {
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar_miskin');
        $this->load->view('kemiskinan/v_jumlah', array('list_kecamatan' => $this->penduduk_model->get_list_kecamatan()));
        $this->load->view('theme/footer');
        $this->load->view('js/js_pekerjaan', array(
            'chart' => 'penduduk/chart_persen_daerah'
                )
        );
    }

    function miskin() {
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar_miskin');
        $list_kecamatan = $this->penduduk_model->get_list_kecamatan();
        $this->load->view('kemiskinan/v', array('list_kecamatan' => $list_kecamatan));
        $this->load->view('theme/footer');
        $this->load->view('js/js_miskin', array(
            'chart' => 'penduduk/chart_miskin'
                )
        );
    }

    function miskin_keterangan() {
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar_miskin');
        $this->load->view('kemiskinan/v', array('list_kecamatan' => $this->penduduk_model->get_list_kecamatan()));
        $this->load->view('theme/footer');
        $this->load->view('js/js_miskin', array('chart' => 'penduduk/chart_miskin_keterangan'));
    }

    function persen_miskin() {
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar_miskin');
        $this->load->view('kemiskinan/v', array('list_kecamatan' => $this->penduduk_model->get_list_kecamatan()));
        $this->load->view('theme/footer');
        $this->load->view('js/js_miskin', array(
            'chart' => 'penduduk/chart_persen_miskin'
                )
        );
    }

    function chart_persen_miskin() {
        $this->load->model('kemiskinan_model');
        $id_kec = $this->input->post('id_kecamatan');
        $id_kel = $this->input->post('id_kelurahan');
        $query = $this->kemiskinan_model->getChart($id_kec, $id_kel);
        $jumlah_penduduk = $this->penduduk_model->getJumlahPenduduk($id_kec,$id_kel);
        $subtitle = array("BANYUWANGI");
        $kec = $this->penduduk_model->get_kec_nama($id_kec);
        if (strlen($kec) > 0) {
            $subtitle[] = $kec;
            $kel = $this->penduduk_model->get_kel_nama($id_kec, $id_kel);
            if (strlen($kel)) {
                $subtitle[] = $kel;
            }
        }
        $l = count($subtitle);
        $this->load->view('chart/pie_persen_daerah', array(
            'data' => $query,
            'judul' => 'Prosentase Penduduk Miskin Per Daerah',
            'subtitle' => $subtitle,
            'kolom' => 'Keterangan',
            'jumlah_semua' => $jumlah_penduduk,
            'kolom2' => 'Jumlah Penduduk Miskin <br/>di ' . ucwords (strtolower($subtitle[$l - 1])),
            'kolom3'=>'Jumlah Penduduk Tidak Miskin <br/>di ' . ucwords (strtolower($subtitle[$l - 1])),
            'controller' => 'penduduk/lihat_miskin'
                )
        );
    }

    function chart_persen_daerah() {
        $this->load->model('kemiskinan_model');
        $id_kec = $this->input->post('id_kecamatan');
        $id_kel = $this->input->post('id_kelurahan');
        $query = $this->penduduk_model->getChart($id_kec, $id_kel);
        $jumlah_penduduk = $this->penduduk_model->getJumlahPenduduk();
        $subtitle = array("BANYUWANGI");
        $kec = $this->penduduk_model->get_kec_nama($id_kec);
        if (strlen($kec) > 0) {
            $subtitle[] = $kec;
            $kel = $this->penduduk_model->get_kel_nama($id_kec, $id_kel);
            if (strlen($kel)) {
                $subtitle[] = $kel;
            }
        }
        $l = count($subtitle);
        $this->load->view('chart/pie_persen_daerah', array(
            'data' => $query,
            'judul' => 'Prosentase Penduduk Per Daerah',
            'subtitle' => $subtitle,
            'kolom' => 'Keterangan',
            'jumlah_semua' => $jumlah_penduduk,
            'kolom2' => 'Jumlah Penduduk di ' . $subtitle[$l - 1],
            'controller' => 'penduduk/lihat',
            'id_kecamatan' => $id_kec,
            'id_kelurahan' => $id_kel
                )
        );
    }

    function chart_miskin_keterangan() {
        $this->load->model('kemiskinan_model');
        $id_kec = $this->input->post('id_kecamatan');
        $id_kel = $this->input->post('id_kelurahan');
        $query = $this->kemiskinan_model->getChartKeterangan($id_kec, $id_kel);
        $subtitle = array("BANYUWANGI");
        $kec = $this->penduduk_model->get_kec_nama($id_kec);
        if (strlen($kec) > 0) {
            $subtitle[] = $kec;
            $kel = $this->penduduk_model->get_kel_nama($id_kec, $id_kel);
            if (strlen($kel)) {
                $subtitle[] = $kel;
            }
        }
        $this->load->view('chart/pie', array(
            'data' => $query,
            'judul' => 'Jumlah Penduduk Miskin berdasarkan Keterangan',
            'subtitle' => $subtitle,
            'kolom' => 'Keterangan',
            'controller' => 'penduduk/lihat_miskin_keterangan',
            'id_kecamatan' => $id_kec,
            'id_kelurahan' => $id_kel
                )
        );
    }

    function chart_miskin() {
        $id_kec = $this->input->post('id_kecamatan');
        $id_kel = $this->input->post('id_kelurahan');
        $this->load->model('kemiskinan_model');
        $query = $this->kemiskinan_model->getChart($id_kec, $id_kel);
        $subtitle = array("BANYUWANGI");
        $kec = $this->penduduk_model->get_kec_nama($id_kec);
        if (strlen($kec) > 0) {
            $subtitle[] = $kec;
            $kel = $this->penduduk_model->get_kel_nama($id_kec, $id_kel);
            if (strlen($kel)) {
                $subtitle[] = $kel;
            }
        }
        $this->load->view('chart/bar_jenis_kelamin', array(
            'data' => $query,
            'judul' => 'Jumlah Penduduk Miskin Berdasarkan Wilayah',
            'kolom_tabel' => 'Wilayah',
            'subtitle' => $subtitle,
            'id_kecamatan' => $id_kec,
            'kolom' => 'Wilayah',
            'controller' => 'penduduk/lihat_miskin',
            'id_kelurahan'=>$id_kel
                )
        );
    }

    function chart() {
        $id_kec = $this->input->post('id_kecamatan');
        $id_kel = $this->input->post('id_kelurahan');
        $query = $this->penduduk_model->getChart($id_kec, $id_kel);
        $subtitle = array("BANYUWANGI");
        $kec = $this->penduduk_model->get_kec_nama($id_kec);
        if (strlen($kec) > 0) {
            $subtitle[] = $kec;
            $kel = $this->penduduk_model->get_kel_nama($id_kec, $id_kel);
            if (strlen($kel)) {
                $subtitle[] = $kel;
            }
        }
        $this->load->view('chart/bar_jenis_kelamin', array(
            'data' => $query,
            'judul' => 'Jenis Kelamin',
            'subtitle' => $subtitle,
            'controller' => 'penduduk/lihat',
            'id_kecamatan' => $id_kec,
            'kolom' => 'Wilayah',
            'id_kelurahan'=>$id_kel
                )
        );
    }

    function lihat_miskin_keterangan() {
        $session = $this->session->userdata('login');
        if ($session == NULL) {
            redirect(site_url() . '/login');
            exit();
        }
        $kunci = (int) $this->input->get('key');
        $limit = (int) $this->input->get('limit');
        $halaman = (int) $this->input->get('halaman');
        $id_kecamatan = (int) $this->input->get('id_kecamatan');
        $id_kelurahan = (int) $this->input->get('id_kelurahan');

        if ($limit > 1000) {
            $limit = 1000;
        } else if ($limit < 10) {
            $limit = 10;
        }
        $this->load->model('kemiskinan_model');
        $jumlah_data = $this->kemiskinan_model->get_jumlah_data($kunci, 
                $id_kecamatan, $id_kelurahan,'keterangan');
        $max_halaman = floor($jumlah_data / $limit);
        if ($halaman < 0) {
            $halaman = 0;
        } else if ($halaman > $max_halaman) {
            $halaman = $max_halaman;
        }

        $list_data = $this->kemiskinan_model->get_data_penduduk($kunci, $limit, 
                $halaman * $limit, $id_kecamatan, $id_kelurahan,'keterangan');
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar_miskin');
        $list_kecamatan = $this->penduduk_model->get_list_kecamatan();
        $list_kelurahan = $this->kemiskinan_model->get_list_kelurahan($id_kecamatan);
        $list_kategori = $this->kemiskinan_model->get_list_master('miskin_master_keterangan');
        $this->load->view('kemiskinan/v_list', array(
            'list_kecamatan' => $list_kecamatan,
            'list_kelurahan' => $list_kelurahan,
            'list_kategori' => $list_kategori,
            'semua_kategori'=>'Semua Keterangan',
            'list_data' => $list_data,
            'id_kecamatan' => $id_kecamatan,
            'id_kelurahan' => $id_kelurahan,
            'key' => $kunci,
            'controller' => 'penduduk/lihat_miskin_keterangan',
            'limit' => $limit,
            'halaman' => $halaman,
            'jumlah_halaman' => $max_halaman,
            'table_header'=>'Keterangan'
                )
        );
    }

    function lihat_miskin() {
        $session = $this->session->userdata('login');
        if ($session == NULL) {
            redirect(site_url() . '/login');
            exit();
        }
        $kunci = (int) $this->input->get('key');
        $limit = (int) $this->input->get('limit');
        $halaman = (int) $this->input->get('halaman');
        $id_kecamatan = (int) $this->input->get('id_kecamatan');
        $id_kelurahan = (int) $this->input->get('id_kelurahan');

        if ($limit > 1000) {
            $limit = 1000;
        } else if ($limit < 10) {
            $limit = 10;
        }
        $this->load->model('kemiskinan_model');
        $jumlah_data = $this->penduduk_model->get_jumlah_data_miskin($kunci - 1, $id_kecamatan, $id_kelurahan);
        $max_halaman = floor($jumlah_data / $limit);
        if ($halaman < 0) {
            $halaman = 0;
        } else if ($halaman > $max_halaman) {
            $halaman = $max_halaman;
        }

        $list_data = $this->penduduk_model->get_data_penduduk_miskin($kunci - 1, $limit, $halaman * $limit, $id_kecamatan, $id_kelurahan);
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar_miskin');
        $list_kecamatan = $this->penduduk_model->get_list_kecamatan();
        $list_kelurahan = $this->kemiskinan_model->get_list_kelurahan($id_kecamatan);
        $list_kategori = $this->penduduk_model->get_list_jenis_kelamin();
        $this->load->view('kemiskinan/v_list_kelamin', array(
            'list_kecamatan' => $list_kecamatan,
            'list_kelurahan' => $list_kelurahan,
            'list_kategori' => $list_kategori,
            'list_data' => $list_data,
            'id_kecamatan' => $id_kecamatan,
            'id_kelurahan' => $id_kelurahan,
            'key' => $kunci,
            'controller' => 'penduduk/lihat_miskin',
            'limit' => $limit,
            'halaman' => $halaman,
            'semua_kategori'=>'Semua Jenis Kelamin',
            'jumlah_halaman' => $max_halaman
                )
        );
    }

    function lihat() {
        $session = $this->session->userdata('login');
        if ($session == NULL) {
            redirect(site_url() . '/login');
            exit();
        }
        $kunci = (int) $this->input->get('key');
        $limit = (int) $this->input->get('limit');
        $halaman = (int) $this->input->get('halaman');
        $id_kecamatan = (int) $this->input->get('id_kecamatan');
        $id_kelurahan = (int) $this->input->get('id_kelurahan');

        if ($limit > 1000) {
            $limit = 1000;
        } else if ($limit < 10) {
            $limit = 10;
        }
        //$this->load->model('pekerjaan_model');
        $jumlah_data = $this->penduduk_model->get_jumlah_data_by_jenis_kelamin(
                $kunci, $id_kecamatan, $id_kelurahan);
        $max_halaman = floor($jumlah_data / $limit);
        if ($halaman < 0) {
            $halaman = 0;
        } else if ($halaman > $max_halaman) {
            $halaman = $max_halaman;
        }

        $list_data = $this->penduduk_model->get_data_penduduk_by_jenis_kelamin($kunci, $limit, $halaman * $limit, $id_kecamatan, $id_kelurahan);
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar');
        $list_kecamatan = $this->penduduk_model->get_list_kecamatan();
        $list_kelurahan = $this->penduduk_model->get_list_kelurahan($id_kecamatan);
        $list_kategori = $this->penduduk_model->get_list_jenis_kelamin();
        $this->load->view('page/v_jenis_kelamin_list', array(
            'list_kecamatan' => $list_kecamatan,
            'list_kelurahan' => $list_kelurahan,
            'list_kategori' => $list_kategori,
            'list_data' => $list_data,
            'id_kecamatan' => $id_kecamatan,
            'id_kelurahan' => $id_kelurahan,
            'key' => $kunci,
            'controller' => 'penduduk/lihat',
            'limit' => $limit,
            'halaman' => $halaman,
            'jumlah_halaman' => $max_halaman
                )
        );
    }

}
